#pragma once
#include <queue>
#include <pthread.h>
#include"basic_buf.hpp"

using std::endl;
using std::cout;

#define BLOCK_QUE_SZ 10
// 缓冲区本身需要维护线程安全
template<typename T>
class BlockQueue : public basic_buf<T>
{
public:
    //声明使用基类的成员变量
    using basic_buf<T>::_mutex_pro_con;
    using basic_buf<T>::_cond_con;
    using basic_buf<T>::_cond_pro;
    using ptr = std::shared_ptr<BlockQueue<T>>;
    BlockQueue()
    {

    }
    ~BlockQueue()
    {
    }

    virtual void push(const T& val)
    {
        pthread_mutex_lock(&_mutex_pro_con);
        while(full())
            pthread_cond_wait(&_cond_pro,&_mutex_pro_con);
        
        _que.push(val);

        pthread_cond_broadcast(&_cond_con);
        pthread_mutex_unlock(&_mutex_pro_con);

    }

    virtual void pop(T* val)
    {
        pthread_mutex_lock(&_mutex_pro_con);

        while(empty())
            pthread_cond_wait(&_cond_con,&_mutex_pro_con);

        *val = _que.front();
        _que.pop();

        pthread_cond_broadcast(&_cond_pro);

        pthread_mutex_unlock(&_mutex_pro_con);

    }
    virtual bool empty()
    {
        return _que.empty();
    }
    virtual bool full()
    {
        return _que.size() == BLOCK_QUE_SZ;
    }

    virtual void push(const T& val,size_t sz)
    {
        ;
    }
    virtual void pop(T* val,size_t sz)
    {
        ;
    }
private:
    std::queue<T> _que;

};
